#include <cstdio>

using namespace std;

typedef long long ll;
const int maxn=800000, mods=1000000007;

int min(int a, int b) {return a<b ? a : b;}
int max(int a, int b) {return a>b ? a : b;}

int n, son[maxn+1], bro[maxn+1], fa[maxn+1], mark[maxn+1], dfn[maxn+1], size[maxn+1], p[maxn+1];
ll val[4*maxn+1], cnt[4*maxn+1], tag[4*maxn+1], tsum[maxn+1];
bool col[maxn+1];

void add(int o, int l, int r, int tl, int tr, int tv) {
    if (tl>tr) return;
    if (l==tl && r==tr) {
        tag[o]=(tag[o]+tv)%mods;
        val[o]=(val[o]+cnt[o]*tv%mods)%mods;
    } else {
        int mid=(l+r)/2;
        if (tl<=mid) add(o*2, l, mid, tl, min(tr, mid), tv);
        if (tr>mid) add(o*2+1, mid+1, r, max(tl, mid+1), tr, tv);
        val[o] = (val[o*2]+val[o*2+1]+cnt[o]*tag[o]%mods)%mods;
    }
}

void enable(int o, int l, int r, int t) {
    if (l==r) {
        cnt[o] = 1;
        val[o] = tag[o]*cnt[o]%mods;
    } else {
        int mid=(l+r)/2;
        if (t<=mid) enable(o*2, l, mid, t);
        else enable(o*2+1, mid+1, r, t);
        cnt[o] = (cnt[o*2]+cnt[o*2+1])%mods;
        val[o] = (val[o*2]+val[o*2+1]+cnt[o]*tag[o]%mods)%mods;
    }
}

void addSide(int f, int s) {
    fa[s] = f;
    bro[s] = son[f];
    son[f] = s;
}

void build(int o) {
    dfn[o] = ++dfn[0];
    size[o] = 1;
    for (int i=son[o]; i; i=bro[i]) {
        build(i);
        size[o] += size[i];
    }
}

void update(int o) {
    bool flag=false;
    for (; fa[o] && !flag; o=fa[o]) {
        int t=fa[o];
        flag=mark[t];
        if (mark[t]==0) {
            add(1, 1, n, dfn[t]+1, dfn[o]-1, 1);
            add(1, 1, n, dfn[o]+size[o], dfn[t]+size[t]-1, 1);
            p[t] = o;
            mark[t]++;
        } else if (mark[t]==1) {
            add(1, 1, n, dfn[p[t]], dfn[p[t]]+size[p[t]]-1, 1);
            p[t] = -1;
            mark[t]++;
        }
    }
}

int main() {
    freopen("dierti.in", "r", stdin);
    freopen("dierti.out", "w", stdout);

    int root;
    scanf("%d", &n);
    for (int i=1; i<=n; i++) {
        int f;
        scanf("%d", &f);
        if (f) addSide(f, i);
        else root = i;
    }
    build(root);

    ll ans=1;
    for (int i=1; i<=n; i++) {
        col[i] = true;
        enable(1, 1, n, dfn[i]);
        if (!mark[i]) {
            add(1, 1, n, dfn[i], dfn[i]+size[i]-1, 1);
            update(i);
        } else {
            add(1, 1, n, dfn[i], dfn[i], 1);
            if (mark[i]==1) {
                add(1, 1, n, dfn[p[i]], dfn[p[i]]+size[p[i]]-1, 1);
                p[i] = -1;
            }
        }
        mark[i] = 3;
        ans = ans*val[1]%mods;
    }
    printf("%lld\n", ans);

    fclose(stdin);
    fclose(stdout);
    return 0;
}
